/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function (n) {
  let result = []
  let stack = []
  if (n === 0) return result
  function backTracking (left, right) {
    if (left < 0 || right < 0) {
      return
    }
    if (left > right) {
      return
    }
    if (left === 0 && right === 0) {
      result.push([...stack])
      return
    }
    stack.push('(')
    backTracking(left - 1, right)
    stack.pop()
    stack.push(')')
    backTracking(left, right - 1)
    stack.pop()
  }
  backTracking(n, n)
  return result
};